package com.jz.netthings.analysis.service.impl;

import com.jz.netthings.analysis.bean.RoundsBase;
import com.jz.netthings.analysis.mapper.RoundsBaseMapper;
import com.jz.netthings.analysis.service.RoundsBaseService;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.jz.netthings.exception.AdscmSystemException;
import com.jz.netthings.utils.AdscmDateUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.lang.reflect.Method;
import java.util.Calendar;
import java.util.Date;

/**
 * <p>
 * 轮次标准时间表 服务实现类
 * </p>
 *
 * @author yangyunyun
 * @since 2019-07-16
 */
@Service
public class RoundsBaseServiceImpl extends ServiceImpl<RoundsBaseMapper, RoundsBase> implements RoundsBaseService {

    Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    RoundsBaseMapper roundsBaseMapper;

    @Override
    public void loadRoundsBaseByYear(int year) throws AdscmSystemException {

        logger.info("开始构造【"+year+"】年的轮次基本时间表...");
        String beginTime = year + "-01-01 00:00:00";
        String lastEndTime = null;
        for(int i = 1;i< 27;i++){
            RoundsBase base = new RoundsBase();
            base.setRoundIdx(i);
            base.setYear(year);
            base = roundsBaseMapper.selectOne(base);
            boolean isInsert = false;
            if (base == null){
                isInsert = true;
                base = new RoundsBase();
                base.setRoundIdx(i);
                base.setYear(year);
            }
            if (i == 1 ){
                base.setBeginTime(beginTime);
                lastEndTime = AdscmDateUtils.getDateLastSeconds(AdscmDateUtils.getRoundTime(AdscmDateUtils.parseDateL(beginTime),13));
                base.setEndTime(lastEndTime);
            }else if(i < 26) {
                Date d = AdscmDateUtils.parseDateL(lastEndTime);
                Date thatDate = AdscmDateUtils.getThatDate(d, 1, Calendar.DATE);
                lastEndTime = AdscmDateUtils.getDateFirstSeconds(thatDate);

                base.setBeginTime(lastEndTime);
                lastEndTime = AdscmDateUtils.getDateLastSeconds(AdscmDateUtils.getRoundTime(thatDate,13));
                base.setEndTime(lastEndTime);
            }else{
                Date d = AdscmDateUtils.parseDateL(lastEndTime);
                Date thatDate = AdscmDateUtils.getThatDate(d, 1, Calendar.DATE);
                lastEndTime = AdscmDateUtils.getDateFirstSeconds(thatDate);

                base.setBeginTime(lastEndTime);
                lastEndTime = year + "-12-31 23:59:59";
                base.setEndTime(lastEndTime);
            }
            if (isInsert){
                roundsBaseMapper.insert(base);
            }else {
                roundsBaseMapper.updateById(base);
            }

        }



    }


    @Override
    public RoundsBase selectRoundsByDate(String date) throws AdscmSystemException {

        logger.info("获取日期【"+date+"】的轮次");

        return roundsBaseMapper.selectRoundsByDate(date);
    }
}
